package com.SpringBoot.product.service;

import com.SpringBoot.Util.Pager;
import com.SpringBoot.Util.Result;
import com.SpringBoot.product.dto.productDto;
import com.SpringBoot.product.mapper.ProductMapper;
import com.SpringBoot.product.pojo.Product;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bxk
 * @since 2025-09-15
 */
@Service
public class ProductService {

    private final ProductMapper productMapper;

    public ProductService(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    public void addProduct(Product product) {
        productMapper.addProduct(product);
    }

    public void updateProduct(Product product) {
        productMapper.updateProduct(product);
    }

    public Result getProduct( Integer productId, String productName, String name){
        List<productDto> productDto = productMapper.getProduct(productId,productName,name);
        if(productDto.size() <= 0){
            return Result.error(404,"未找到该商品");
        }
        return Result.success(productDto);
    }

    public Result selectProduct(String name ,String color){
        List<productDto> productDto = productMapper.selectProduct(name,color);
        if(productDto.size() <= 0){
            return Result.error(404,"未找到该商品");
        }
        return Result.success(productDto);
    }

    public void deleteProduct(Integer productId) {
        productMapper.deleteProduct(productId);
    }

    public Result selectColor(){
        return Result.success(productMapper.allColor());
    }

    public Result allProductByPage(String productName,String name,Integer pageNum,Integer pageSize){
        //查询总记录数
        Integer recordCount = productMapper.count();
        //构建分页信息对象
        Pager pager = new Pager(pageNum, pageSize, recordCount);
        //根据pager查询需要的数据
        List<productDto> list = productMapper.allProductPage(productName,name,pager.getStart(), pageSize);
        //将分页信息和查询到的数据一起返回到前端
        HashMap<String,Object> map = new HashMap<>();
        map.put("pager",pager);
        map.put("list",list);
        return Result.success(map);
    }

    public Result getCount(){
        return Result.success(productMapper.count());
    }




}
